package top.maof.mfcoin.tx;

import lombok.Data;
import top.maof.mfcoin.algo.SecretUtil;

import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * 交易输出
 *
 * @author 毛逢
 * @date 2021/11/11 11:16
 */
@Data
public class Output {
    private String address;
    // 转多少币
    private int coin;

    public static byte[] hashOutput(Output output) {

        byte[] addressBytes = Base64.getDecoder().decode(output.address);
        byte[] coinBytes = Integer.toBinaryString(output.coin).getBytes();

        byte[] outBytes = new byte[addressBytes.length + coinBytes.length];

        System.arraycopy(addressBytes, 0, outBytes, 0, addressBytes.length);

        System.arraycopy(coinBytes, 0, outBytes, addressBytes.length, coinBytes.length);


        byte[] digest = null;
        try {
            digest = SecretUtil.digest(SecretUtil.DIGEST_ALGORITHM_SHA256, outBytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return digest;
    }
}
